﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/*使用简单工厂模式设计一个可以创建不同几何图形（Shape），如Circle，Rectangle，Triangle等绘图工具类，每个几何图形均具有绘制Draw()和擦除Erase()两个方法
 * 要求在绘制不支持的几何图形时，抛出一个UnsuppShapeException异常，绘制类图并使用C#语言实现。
*/

namespace project4
{
    public interface InShape//图形接口 抽象产品类
    {
        void Draw();
        void Erase();
    }

    public class Circle : InShape//圆形类，具体产品类
    {
        private static int count;   //生成图形计数
        string radious;//半径
        public Circle()//构造
        {
            Console.WriteLine("Create Circle");
            Console.WriteLine("Input the radious of Circle:");
            radious = Console.ReadLine();
        }
        public void Draw()//实现接口方法
        {
            int Radious = int.Parse(radious);
            Console.WriteLine("Display circle " + (++count) + " information:");
            Console.WriteLine("Circle " + count + " circumference：" + 2 * Radious * 3.14159);
            Console.WriteLine("Circle " + count + " area：" + 3.14159 * Radious * Radious);
        }
        public void Erase()//实现接口方法
        {
            while (true)
            {
                Console.WriteLine("Erase current shape（y/n）？");
                string choose;
                choose = Console.ReadLine();
                if (choose.Equals("y") || choose.Equals("Y"))
                {
                    Console.WriteLine("Erase Circle " + count + " successfully!");
                    count--;
                    break;
                }
                else if (choose.Equals("n") || choose.Equals("N"))
                {
                    Console.WriteLine("Circle " + count + " successfully saved!");
                    break;
                }
                else
                {
                    Console.WriteLine("Input error, re-enter!");
                }
            }
        }
    }

    class Rectangle : InShape//矩形类，具体产品类
    {
        private static int count = 0;//生成图形计数
        string length;
        string wideth;
        public Rectangle()//构造
        {
            Console.WriteLine("Create Rectangle");
            Console.WriteLine("Input the length and wideth of Rectangle:");
            length = Console.ReadLine();
            wideth = Console.ReadLine();
        }
        public void Draw()//实现接口方法
        {
            int Length = int.Parse(length);
            int Wideth = int.Parse(wideth);
            Console.WriteLine("Display rectangle " + (++count) + " information:");
            Console.WriteLine("Rectangle " + count + "circumference：" + 2 * Length * Wideth);
            Console.WriteLine("Rectangle " + count + "area：" + Length * Wideth);

        }
        public void Erase()//实现接口方法
        {
            while (true)
            {
                Console.WriteLine("Erase current shape（y/n）？");
                string choose;
                choose = Console.ReadLine();
                if (choose.Equals("y") || choose.Equals("Y"))
                {
                    Console.WriteLine("Erase rectangle " + count + "successfully!");
                    --count;
                    break;
                }
                else if (choose.Equals("n") || choose.Equals("N"))
                {
                    Console.WriteLine("Rectangle " + count + " successfully saved!");
                    break;
                }
                else
                {
                    Console.WriteLine("Input error, re-enter!");
                }
            }
        }
    }

    class Triangle : InShape//三角形类，具体产品类
    {
        private static int count = 0;//生成图形计数
        string lengtha;
        string lengthb;
        string lengthc;
        public Triangle()//构造
        {
            Console.WriteLine("Create Triangle");
            Console.WriteLine("Input the lengtha ,lengthb and lengthc of Triangle:");
            lengtha = Console.ReadLine();
            lengthb = Console.ReadLine();
            lengthc = Console.ReadLine();

        }
        public void Draw()//实现接口方法
        {
            int Lengtha = int.Parse(lengtha);
            int Lengthb = int.Parse(lengthb);
            int Lengthc = int.Parse(lengthc);
            if ((Lengtha + Lengthb > Lengthc) && (Lengtha + Lengthc > Lengthb) && (Lengthb + Lengthc > Lengtha))
            {
                double S = (Lengtha + Lengthb + Lengthc) * 0.5;
                double area = Math.Sqrt(S * (S - Lengtha) * (S - Lengthb) * (S - Lengthc));
                Console.WriteLine("Display triangle " + (++count) + " information:");
                Console.WriteLine("Triangle " + count + " circumference：" + (Lengtha + Lengthb + Lengthc));
                Console.WriteLine("Triangle " + count + " area：" + area);
                Erase();
            }
            else
            {
                Console.WriteLine("Create triangle failed!");
            }
        }
        public void Erase()//实现接口方法
        {
            while (true)
            {
                Console.WriteLine("Erase shape（y/n）？");
                string choose;
                choose = Console.ReadLine();
                if (choose.Equals("y") || choose.Equals("Y"))
                {
                    Console.WriteLine("Erase tirangle " + count + " successfully!");
                    --count;
                    break;
                }
                else if (choose.Equals("n") || choose.Equals("N"))
                {
                    Console.WriteLine("Triangle " + count + " successfully saved!");
                    break;
                }
                else
                {
                    Console.WriteLine("Input error, re-enter!");
                }
            }
        }
    }

    class ShapeFactory//图形工厂类，充当工厂类
    {
        public static InShape Getshape(string type)//静态工厂方法
        {
            InShape shape;
            shape = null;
            if (type.Equals("Circle"))
            {
                shape = new Circle();
                Console.WriteLine("Init set Circle");
                shape.Draw();
                shape.Erase();
            }
            else if (type.Equals("Rectangle"))
            {
                shape = new Rectangle();
                Console.WriteLine("Init set Rectangle");
                shape.Draw();
                shape.Erase();
            }
            else if (type.Equals("Triangle"))
            {
                shape = new Triangle();
                Console.WriteLine("Init set Triangle");
                shape.Draw();

            }
            else//异常 
            {
                Console.WriteLine("UnsupportShapeException!");
                Console.WriteLine("Please re-enter:");
            }
            return shape;
        }
    }

    class Program//客户端测试类
    {
        static void Main(string[] args)
        {
            while (true)
            {
                InShape shape;
                Console.WriteLine("Please input the shape you want to create");
                string str = Console.ReadLine();
                shape = ShapeFactory.Getshape(str);//通过静态工厂方法创建产品
                Console.ReadLine();
            }
        }
    }
}